package org.example.struct.array;

import com.alibaba.fastjson.JSON;

import java.util.Arrays;

/**
 * @author liyishan
 * @date 2023/8/10 11:02
 * @apiNote
 */

public class Test00977SortedSquares {
    public static void main(String[] args) {
        int[] nums = new int[]{-4,-1,0,3,10};
        int[] res = test(nums);
        System.out.println(JSON.toJSON(res));
    }
    public static int[] test(int[] nums) {
        int[] ans = new int[nums.length];
        for (int i = 0; i < nums.length; ++i) {
            ans[i] = nums[i] * nums[i];
        }
        Arrays.sort(ans);
        return ans;
    }

    public static int[] test1(int[] nums) {
        int n = nums.length;
        int[] ans = new int[n];
        for (int i = 0, j = n - 1, pos = n - 1; i <= j;) {
            if (nums[i] * nums[i] > nums[j] * nums[j]) {
                ans[pos] = nums[i] * nums[i];
                ++i;
            } else {
                ans[pos] = nums[j] * nums[j];
                --j;
            }
            --pos;
        }
        return ans;
    }
}
